from django.shortcuts import render, redirect
from app01 import models
from app01.utils.pagination import Pagination

from app01.utils.form import UserModelForm,MobileModelForm,MobileeditModelForm

# Create your views here.

def user_list(request):
    """用户管理"""
    queryset = models.UserInfo.objects.all()

    ## 分页
    page_object = Pagination(request,queryset,page_size=2)
    context = {
         'queryset':page_object.page_queryset,
          'page_string':page_object.html(),
    }
    return render(request,'user_list.html',context)



    return render(request,'user_list.html',{'queryset':queryset})

def user_add(request):
    """添加用户(原始方法)"""
    if request.method == "GET":
        context = {
            'gender_choices': models.UserInfo.gender_choices,
            'depart_list': models.Department.objects.all(),
        }
        return render(request,'user_add.html',context)
    # 获取用户提交的数据
    user = request.POST.get('user')
    pwd = request.POST.get('pwd')
    age = request.POST.get('age')
    account = request.POST.get('ac')
    ctime = request.POST.get('ctime')
    gender_id = request.POST.get('gd')
    depart_id = request.POST.get('dp')
    #添加到数据库中
    models.UserInfo.objects.create(name = user,password=pwd, age=age, account=account, create_time=ctime, gender=gender_id, depart_id=depart_id)

    #添加成功后，返回到用户列表中
    return redirect("/user/list/")
######################################Model Form 实例 #####################################

def user_model_form_add(request):
    """添加用户(ModelForm版本)"""
    if request.method == "GET":
        form = UserModelForm()
        return render(request, 'user_model_form_add.html', {'form':form})
    # 用户post提交数据 数据集检验
    form = UserModelForm( data = request.POST)
    if form.is_valid():
    # print(form.cleaned_data)
        # 如果数据合法，保存到数据库
        #models.UserInfo.objects.create()
        form.save()
        return redirect("/user/list/")
    # 校验失败，在页面上显示错误信息
    return render(request, 'user_model_form_add.html', {'form':form})

def user_edit(request,nid):
    """编辑用户"""
    row_object = models.UserInfo.objects.filter(id=nid).first()
    if request.method == "GET":
        """设置编辑界面每一项的默认值，根据id去数据库获取要编辑的哪一行数据"""

        form = UserModelForm(instance=row_object)
        return render(request, 'user_edit.html', {'form':form})

    # 要指明将数据更新到nid中，否则mdelform会在list中新增一个数据
    form = UserModelForm( data = request.POST, instance = row_object)
    if form.is_valid():
        # 默认保存的是用户输入的所有数据，如果想要在用户输入以外增加一点值
        # form.instance.字段名 = 值
        form.save()
        return redirect("/user/list/")
    return render(request, 'user_edit.html', {'form':form})
def user_delete(request,nid):
    models.UserInfo.objects.filter(id=nid).delete()
    return redirect("/user/list/")